/**
 * 
 */
package name.yzhu.httpserver.method;

import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.yzhu.common.lang.LangUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.common.base.Splitter;

/**
 * a method description which is using Annotation {@link HttpMethodAnno}
 * @author yhzhu
 *
 */
public class MethodDelcleare {
	private static final Log LOG = LogFactory.getLog(MethodDelcleare.class);
	
    private String hrm;
    private String methodName;
    private List<String> parameterNames;
    private String contentType;
    private String errorCode;
    private boolean asyncMethod=false;
    private boolean naiveMethod = false;
	private IMethodAuthorized methodAuthorized = null;
    
    public static MethodDelcleare getInstance(Method method){
    	HttpMethodAnno hma = method.getAnnotation(HttpMethodAnno.class);
    	if (hma == null)
    		throw new NoSuchMethodError("there is no anntation HttpMethodAnno in "+method.getName());
    	
    	MethodDelcleare md = new MethodDelcleare();
    	md.setMethodName(method.getName());
    	md.setHrm(hma.hrm());
    	md.setAsyncMethod(hma.async());
    	md.setContentType(hma.contentType());
    	md.setNaiveMethod(hma.naive());
    	String ps = hma.parameters();
    	md.setParameterNames(Splitter.on(',').trimResults().splitToList(ps));
    	    	
    	String cls = hma.authorized();
    	if (cls == null || cls.trim().equals("")){
    		md.setMethodAuthorized(new DefaultMethodAuthorized());
    	}
    	else {
    		try {
    			Object auth = LangUtil.getClassByName(cls, MethodDelcleare.class.getClassLoader());
				md.setMethodAuthorized((IMethodAuthorized) auth);
			} catch (Exception e) {
				LOG.warn("No find the class name "+cls+" use default authorized instead  of");
				md.setMethodAuthorized(new DefaultMethodAuthorized());
			}
    	}
		return md;    	
    }
    
	public void setMethodAuthorized(IMethodAuthorized author) {
		this.methodAuthorized  = author;
	}
	
	public IMethodAuthorized getMethodAuthorized() {
		return this.methodAuthorized;
	}
	
	public String getHrm() {
		return hrm;
	}
	public void setHrm(String hrm) {
		this.hrm = hrm;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
	public boolean isAsyncMethod() {
		return asyncMethod;
	}
	public void setAsyncMethod(boolean asyncMethod) {
		this.asyncMethod = asyncMethod;
	}
	public void setNaiveMethod(boolean naiveMethod) {
		this.naiveMethod = naiveMethod;
	}
	public boolean isNaiveMethod() {
		return naiveMethod;
	}
	
	public List<String> getParameterNames() {
		return parameterNames;
	}

	public void setParameterNames(List<String> parameterNames) {
		this.parameterNames = parameterNames;
	}

	public static class DefaultMethodAuthorized implements IMethodAuthorized{

		@Override
		public boolean check(HttpServletRequest req, 
				HttpServletResponse res, MethodDelcleare md) {
			return true;
		}
		
	}
}
